package com.backend.system.common.processor;

import com.backend.system.common.annotation.ProcessorBean;
import com.backend.system.service.*;
import com.backend.system.util.SpringBeanUtils;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 处理器模板
 *
 * @author feng
 */
@Slf4j
public abstract class AbstractProcessorTemplate<T, R> implements Processor<T, R> {

    @Resource
    protected IUserService userService;

    @Resource
    protected IRoleService roleService;

    @Resource
    protected IRoleUserService roleUserService;

    @Resource
    protected IPermissionService permissionService;

    @Resource
    protected IMenuService menuService;

    @Resource
    protected IMenuRoleService menuRoleService;

    @Resource
    protected IRolePermissionService rolePermissionService;

    @Resource
    protected IDictionaryService dictionaryService;


    protected void init(ProcessorContext<T> context) {
    }

    protected void after(ProcessorContext<T> context, R result) {
    }

    public R start(ProcessorContext<T> context) {
        init(context);

        AbstractProcessorTemplate<T, R> template = SpringBeanUtils.getByClass(this.getClass());
        R result = template.handle(context);

        after(context, result);
        return result;
    }


    @PostConstruct
    private void registerProcessor() {
        if (this.getClass().isAnnotationPresent(ProcessorBean.class)) {
            ProcessorBean processorBean = this.getClass().getDeclaredAnnotation(ProcessorBean.class);

            log.info("ProcessorBean Register, action is {}, processor is {}", processorBean.action(), this.getClass().getName());
            ProcessorFactory.register(processorBean.action(), this);
        }
    }
}
